package handler

import (
	"github.com/starmotioncf/soutchatinternal/resp"
	"github.com/starmotioncf/soutchatinternal/service"

	"github.com/gin-gonic/gin"
)

type ThirdGameHandler struct {
	*Handler
	thirdGameService service.ThirdGameService
}

func NewThirdGameHandler(h *Handler, thirdGameService service.ThirdGameService) *ThirdGameHandler {
	return &ThirdGameHandler{
		Handler:          h,
		thirdGameService: thirdGameService,
	}
}

func (h *ThirdGameHandler) GetUserInfo(ctx *gin.Context) {
	accountId := ctx.Param("account_id")
	plat, err := h.thirdGameService.GetPlatformProvider(accountId)
	if err != nil {
		resp.Abort(ctx, 400, err)
		return
	}
	data, err := plat.UserInfo(ctx, ctx.Request)
	if err != nil {
		resp.Abort(ctx, 500, err)
		return
	}
	resp.Success(ctx, data)
}

func (h *ThirdGameHandler) ChangeGold(ctx *gin.Context) {
	accountId := ctx.Param("account_id")
	plat, err := h.thirdGameService.GetPlatformProvider(accountId)
	if err != nil {
		resp.Abort(ctx, 400, err)
		return
	}
	data, err := plat.ChangeGold(ctx, ctx.Request)
	if err != nil {
		resp.Abort(ctx, 500, err)
		return
	}
	resp.Success(ctx, data)
}

func (h *ThirdGameHandler) DepositGold(ctx *gin.Context) {
	accountId := ctx.Param("account_id")
	plat, err := h.thirdGameService.GetPlatformProvider(accountId)
	if err != nil {
		resp.Abort(ctx, 400, err)
		return
	}
	data, err := plat.DepositGold(ctx, ctx.Request)
	if err != nil {
		resp.Abort(ctx, 500, err)
		return
	}
	resp.Success(ctx, data)
}

func (h *ThirdGameHandler) ConsumeGold(ctx *gin.Context) {
	accountId := ctx.Param("account_id")
	plat, err := h.thirdGameService.GetPlatformProvider(accountId)
	if err != nil {
		resp.Abort(ctx, 400, err)
		return
	}
	data, err := plat.ConsumeGold(ctx, ctx.Request)
	if err != nil {
		resp.Abort(ctx, 500, err)
		return
	}
	resp.Success(ctx, data)
}

func (h *ThirdGameHandler) GetToken(ctx *gin.Context) {
	accountId := ctx.Param("account_id")
	plat, err := h.thirdGameService.GetPlatformProvider(accountId)
	if err != nil {
		resp.Abort(ctx, 400, err)
		return
	}
	data, err := plat.CreateToken(ctx, ctx.Request)
	if err != nil {
		resp.Abort(ctx, 500, err)
		return
	}
	resp.Success(ctx, data)
}

func (h *ThirdGameHandler) RefreshToken(ctx *gin.Context) {
	accountId := ctx.Param("account_id")
	plat, err := h.thirdGameService.GetPlatformProvider(accountId)
	if err != nil {
		resp.Abort(ctx, 400, err)
		return
	}
	data, err := plat.RefreshToken(ctx, ctx.Request)
	if err != nil {
		resp.Abort(ctx, 500, err)
		return
	}
	resp.Success(ctx, data)
}
